import random

import pygame
import sys

color1 = (0, 128, 128)
color2 = (160, 240, 30)
color3 = (255, 0, 0)

enemy = [pygame.image.load(r'C:\Users\hp\Desktop\敌人2.png'), pygame.image.load(r'C:\Users\hp\Desktop\敌人3.png'),
         pygame.image.load(r'C:\Users\hp\Desktop\敌人4.png'), pygame.image.load(r'C:\Users\hp\Desktop\敌人5.png'),
         pygame.image.load(r'C:\Users\hp\Desktop\敌人1.png')]

# 初始化
pygame.init()

# 标题
pygame.display.set_caption('飞机大战')

# 窗口
screen_width, screen_height = 800, 600
screen_image = pygame.display.set_mode((screen_width, screen_height))
screen_rect = screen_image.get_rect()

# 飞船
ship_image = pygame.image.load(r'C:\Users\hp\Desktop\飞机.png')
ship_rect = ship_image.get_rect()
ship_rect.x = (screen_width - ship_rect.width) // 2
ship_rect.y = screen_height - ship_rect.height - 5

# 敌人




enemy_image = pygame.image.load(r'C:\Users\hp\Desktop\敌人1.png')
enemy_rect = enemy_image.get_rect()
enemy_rect.x = random.randint(0, screen_width - enemy_rect.width)
enemy_rect.y = -enemy_rect.height

# 子弹
buttlt_rect = pygame.Rect(0, 0, 3, 15)
buttlt_rect.midbottom = ship_rect.midtop

# 速度
ship_speed = 5
enemy_speed = 2

# 背景
bg_image = pygame.image.load(r'C:\Users\hp\Desktop\背景.png')
bg_image_rect = bg_image.get_rect()
bg_image_width, bg_image_height = screen_image.get_size()
image = pygame.transform.scale(bg_image, (screen_width, screen_height))

# 文字
txt_font = pygame.font.SysFont(None, 48)
txt_image = txt_font.render('plane battle', True, color2, color3)
txt_rect = txt_image.get_rect()

# 死循环
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        # 获取按键
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                ship_rect.x -= 10
            if event.key == pygame.K_RIGHT:
                ship_rect.x += 10
            if event.key == pygame.K_UP:
                ship_rect.y -= 10
            if event.key == pygame.K_DOWN:
                ship_rect.y += 10
            if event.key == pygame.K_SPACE:
                buttlt_rect.midbottom = ship_rect.midtop

        # 限制飞机移动范围
        if ship_rect.x < 0:
            ship_rect.x = 0
        if ship_rect.x > screen_width - ship_rect.width:
            ship_rect.x = screen_width - ship_rect.width
        if ship_rect.y < 0:
            ship_rect.y = 0
        if ship_rect.y > screen_height - ship_rect.height:
            ship_rect.y = screen_height - ship_rect.height

        # 敌机移动
        enemy_rect.y += enemy_speed
        if enemy_rect.y > screen_height:
            enemy_rect.x = random.randint(0, screen_width - enemy_rect.width)
            enemy_rect.y = -enemy_rect.height
    # 子弹速度
    buttlt_rect.y -= 1

    # 加载音乐，并对音乐做设置
    pygame.mixer.music.load(r'C:\Users\hp\Desktop\个人\音乐\伴奏\琴.mp3')  # 载入音乐
    pygame.mixer.music.play(3)  # 播放4次
    pygame.mixer.music.queue(
        r'C:\Users\hp\Desktop\个人\音乐\伴奏\鼓.mp3')  # 加载到播放序列，循环播放       将音乐放到播放列表          前面的减一播放前面的，最后一个播放后面的
    # 绘制图像
    # 底色
    screen_image.fill(color1)
    # 背景
    screen_image.blit(bg_image, bg_image_rect)
    # 屏幕与背景自适应
    # 缩放图片到窗口大小
    image = pygame.transform.scale(image, (screen_width, screen_height))
    # 获取图片的大小
    image_width, image_height = image.get_size()
    # 计算图片在窗口中的位置
    image_x = (screen_width - image_width) // 2
    image_y = (screen_height - image_height) // 2
    # 在窗口中央显示图片
    screen_image.blit(image, (image_x, image_y))
    # 飞船
    screen_image.blit(ship_image, ship_rect)
    # 绘制子弹
    pygame.draw.rect(screen_image, color2, buttlt_rect)

    # 文本信息
    screen_image.blit(txt_image, txt_rect)
    # 敌人
    screen_image.blit(enemy_image, enemy_rect)
    # 敌机移动
    enemy_rect.y += enemy_speed
    if enemy_rect.y > screen_height:
        enemy_rect.x = random.randint(0, screen_width - enemy_rect.width)
        enemy_rect.y = -enemy_rect.height
    pygame.display.flip()
